package Test;

import com.mapper.WorkerMapper;
import com.pojo.Worker;
import com.utlis.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

public class WorkerTest {
    @Test
    public void findWorkerByIdTest() {
        SqlSession sqlSession = MyBatisUtils.getSession();
        WorkerMapper workerMapper = sqlSession.getMapper(WorkerMapper.class);
        Worker worker = workerMapper.selectWorkerById(1);
        System.out.println(worker.toString());
        sqlSession.close();
    }
    @Test
    public void insertWorkerTest() {
        SqlSession sqlSession = MyBatisUtils.getSession();
        Worker worker = new Worker();
        worker.setId(4);
        worker.setName("赵六");
        worker.setAge(36);
        worker.setSex("男");
        worker.setWorkerId(1004);
        WorkerMapper mapper = sqlSession.getMapper(WorkerMapper.class);
        int result = mapper.insertWorker(worker);
        System.out.println(result);
        System.out.println(worker.toString());
        sqlSession.commit();
        sqlSession.close();
    }
    @Test
    public void updateWorkerTest() {
        SqlSession sqlSession = MyBatisUtils.getSession();
        Worker worker = new Worker();
        worker.setId(4);
        worker.setName("李华");
        worker.setAge(28);
        WorkerMapper mapper = sqlSession.getMapper(WorkerMapper.class);
        int result = mapper.updateWorker(worker);
        if (result > 0) {
            System.out.println("更新成功");
        }else {
            System.out.println("更新失败");
        }
        System.out.println(worker.toString());
        sqlSession.commit();
        sqlSession.close();
    }
    @Test
    public void deleteWorkerTest() {
        SqlSession sqlSession = MyBatisUtils.getSession();
        WorkerMapper mapper = sqlSession.getMapper(WorkerMapper.class);
        int result = mapper.deleteWorker(4);
        if (result > 0) {
            System.out.println("删除成功");
        }else {
            System.out.println("删除失败");
        }
        sqlSession.commit();
        sqlSession.close();
    }
    @Test
    public void selectWorkerByIdAndNameTest() {
        SqlSession sqlSession = MyBatisUtils.getSession();
        WorkerMapper mapper = sqlSession.getMapper(WorkerMapper.class);
        Worker worker = mapper.selectWorkerByIdAndName(3, "王五");
        System.out.println(worker.toString());
        sqlSession.close();
    }
}